//
//  ASUserDetailMenuView.m
//  AvatarShow
//
//  Created by xunruiIOS on 2021/5/6.
//

#import "ASUserDetailMenuView.h"

static NSString *ASUserDetailMenuCellStr = @"ASUserDetailMenuCell";
@interface ASUserDetailMenuCell : UICollectionViewCell
@property (strong, nonatomic) UIImageView *iconImage;
@property (strong, nonatomic) UILabel *titleLab;
@end
@implementation ASUserDetailMenuCell
-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self.contentView addSubview:self.iconImage];
        [self.contentView addSubview:self.titleLab];
        [self setAllViewLayout];
    }
    return self;
}

- (void)setAllViewLayout{
    @weakify(self);
    [self.iconImage mas_makeConstraints:^(MASConstraintMaker *make) {
        @strongify(self);
        make.top.mas_equalTo(2);
        make.centerX.mas_equalTo(self.contentView.mas_centerX);
    }];
    [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        @strongify(self);
        make.centerX.mas_equalTo(self.contentView.mas_centerX);
        make.top.mas_equalTo(self.iconImage.mas_bottom).mas_offset(10);
    }];
}
-(UIImageView *)iconImage{
    if (!_iconImage) {
        _iconImage = [UIImageView new];
    }
    return _iconImage;
}

- (UILabel *)titleLab{
    if (!_titleLab) {
        _titleLab = [UILabel new];
        _titleLab.font = [UIFont systemFontOfSize:13];
        _titleLab.textColor = [UIColor colorWithHexString:@"#333333"];
        _titleLab.textAlignment = NSTextAlignmentCenter;
    }
    return _titleLab;
}

@end

@implementation ASUserDetailMenuView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.imageArr = [NSMutableArray arrayWithArray:@[@"details_user_fx"]];
        self.titleArr = [NSMutableArray arrayWithArray:@[@"分享"]];
        [self addSubview:self.collectionView];
        [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(0);
            make.left.mas_equalTo(20);
            make.right.mas_equalTo(-20);
            make.bottom.mas_equalTo(-Height_SafeAreaBottom);
        }];
    }
    return self;
}

- (UICollectionView *)collectionView{
    if (!_collectionView) {
        UICollectionViewFlowLayout *fl = [UICollectionViewFlowLayout new];
        fl.minimumLineSpacing = 0;
        fl.minimumInteritemSpacing = 0;
        fl.itemSize = CGSizeMake((SCREEN_WIDTH-40)/self.imageArr.count, 75);
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:fl];
        _collectionView.backgroundColor = self.backgroundColor;
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        [_collectionView registerClass:[ASUserDetailMenuCell class] forCellWithReuseIdentifier:ASUserDetailMenuCellStr];
    }
    return _collectionView;
}

#pragma mark - UICollectionViewDelegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.imageArr.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    ASUserDetailMenuCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ASUserDetailMenuCellStr forIndexPath:indexPath];
    if (self.imageArr.count > indexPath.row) {
        [cell.iconImage setImage:[UIImage imageNamed:self.imageArr[indexPath.row]]];
    }
    if (self.titleArr.count > indexPath.row) {
        cell.titleLab.text = self.titleArr[indexPath.row];
    }
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (self.clickSubMenuBlock) {
        self.clickSubMenuBlock(indexPath.row);
    }
}

@end
